package com.ruoyi.galaxy.api.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.galaxy.api.AbstractGalaxyApi;
import com.ruoyi.galaxy.vo.order.GalaxyApiOrderListResponse;
import com.ruoyi.galaxy.vo.GalaxyApiResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yeoman
 * @date 2025/4/10 21:22
 * @description
 */
public class GalaxyApiOrder extends AbstractGalaxyApi {

    @Override
    protected <T> Map<String, Object> setUriVariables(T request) {
        Map<String, Object> uriVariables = new HashMap<>(5);
        uriVariables.put("pageIndex", TableSupport.buildPageRequest().getPageNum());
        uriVariables.put("pageSize", TableSupport.buildPageRequest().getPageSize());
        uriVariables.put("needCount", true);
        uriVariables.put("orderListType", 1);
        uriVariables.put("langType", 1);
        return uriVariables;
    }

    @Override
    protected GalaxyApiResponse parseResponseData(JSONObject data) {
        GalaxyApiOrderListResponse response = new GalaxyApiOrderListResponse();
        List<GalaxyApiOrderListResponse.GalaxyOrder> list = new ArrayList<>();
        JSONArray dataList = data.getJSONArray("dataList");
        dataList.forEach(order -> {
            JSONObject orderJson = (JSONObject)JSON.parse(JSON.toJSONString(order));
            GalaxyApiOrderListResponse.GalaxyOrder galaxyOrder = JSON.parseObject(JSON.toJSONString(orderJson), GalaxyApiOrderListResponse.GalaxyOrder.class);
            JSONArray orderEventVoList = orderJson.getJSONArray("orderEventVoList");
            JSONObject orderEventJson = (JSONObject)orderEventVoList.get(0);
            GalaxyApiOrderListResponse.GalaxyOrder.GalaxyOrderEvent galaxyOrderEvent = JSON.parseObject(JSON.toJSONString(orderEventJson), GalaxyApiOrderListResponse.GalaxyOrder.GalaxyOrderEvent.class);
            galaxyOrder.setOrderEvent(galaxyOrderEvent);
            list.add(galaxyOrder);
        });
        response.setList(list);
        response.setTotal(data.getInteger("totalRow"));
        return response;
    }

}
